package com.choosefine.springboot.devops.demo.beetlSql;

import com.choosefine.springboot.devops.demo.beetlSql.quhengOrder.QuHengOrder;
import com.choosefine.springboot.devops.demo.beetlSql.quhengOrder.QuHengOrderService;
import com.choosefine.springboot.devops.demo.beetlSql.user.User;
import com.choosefine.springboot.devops.demo.beetlSql.user.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/v1/springboot/demo/beetlSQL")//这里是整个控制器公用的url部分
public class BeetlSqlController {
    @Autowired
    UserService userService;
    @Autowired
    QuHengOrderService orderService;
    @Autowired
    ObjectMapper jsonObjectMapper;

    @RequestMapping("/demo.html")//这个是当前的url特有的url部分
    public ModelAndView home(User user) {//ModelAndView就是返回模板和视图
        ModelAndView view = new ModelAndView("/v1/springboot/demo/beetlSQL/demo.html");
        List list = userService.allUser(user);
        view.addObject("list", list);
        return view;
    }

    @GetMapping("/user")//这个是当前的url特有的url部分
    public String userList(User user) throws JsonProcessingException {//ModelAndView就是返回模板和视图
        List list = userService.allUser(user);
        return jsonObjectMapper.writeValueAsString(list);
    }

    @GetMapping("/quhengOrder")//这个是当前的url特有的url部分
    public String quhengOrder(QuHengOrder order) throws JsonProcessingException {//ModelAndView就是返回模板和视图
        List list = orderService.selectOrder(order);
        return jsonObjectMapper.writeValueAsString(list);
    }
}
